package com.party.admin.web.controller.mall;

import com.party.admin.biz.mall.MallShowGoodsBizService;
import com.party.admin.web.dto.AjaxResult;
import com.party.common.paging.Page;
import com.party.core.model.mall.MallShowGoods;
import com.party.core.model.mall.MallShowGoodsWithAuthor;
import com.party.core.service.mall.IMallShowCategoryService;
import com.party.core.service.mall.IMallShowGoodsService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 商品导购
 * @author cyl
 *
 */
@Controller
@RequestMapping("/mall/showtemplategoods")
public class MallShowTemplateGoodsController {
	//微网站域名
	@Value("#{party['shop.url']}")
	private String domain;

	private String shopGoodsDetail = "goods/";

	private String shopClassifyDetail = "classify/";

	protected static Logger logger = LoggerFactory.getLogger(MallShowTemplateGoodsController.class);

	@Autowired
	private IMallShowCategoryService mallShowCategoryService;
	
	@Autowired
	private IMallShowGoodsService mallShowGoodsService;

	@Autowired
	private MallShowGoodsBizService mallShowGoodsBizService;

	
	@RequestMapping(value="list")
	public ModelAndView showGoodsList(MallShowGoodsWithAuthor mallShowGoodsWithAuthor,Page page,String templateId) {
		ModelAndView mv = new ModelAndView("mall/showGoodsList");
		mv.addObject("page", page);
		mv.addObject("templateId", templateId);
		mv.addObject("categoryId",mallShowGoodsWithAuthor.getCategoryId());
		List<MallShowGoodsWithAuthor> list = mallShowGoodsBizService.list(mallShowGoodsWithAuthor, page);
		mv.addObject("goodsList", list);
		return mv;
	}
	
	//编辑或发布新的导购商品
	@RequestMapping(value="showgoodform")
	public ModelAndView showGoodForm(String id,String templateId,String categoryId) {
		ModelAndView mv = new ModelAndView("mall/showGoodForm");
		mv.addObject("templateId", templateId);
		mv.addObject("categoryId", categoryId);
		//指定商品种类
		mv.addObject("category", mallShowCategoryService.get(categoryId));
		
		if(StringUtils.isNotEmpty(id)) {
			MallShowGoods showGood = mallShowGoodsService.get(id);
			mv.addObject("showGood", showGood);
		}
		
		return mv;
	}

	/**
	 *
	 * @param targetId
	 * @param type: 1-专题, 2-商品, 3-分类, 4-链接; 现在只用 2, 3
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getUrl")
	public AjaxResult getUrl(String targetId, Integer type) {
		try {
			StringBuilder url = new StringBuilder("").append(domain);
			if (StringUtils.isEmpty(targetId) || null == type) {
				return AjaxResult.error("targetId或type不能为空");
			}
			if (Integer.valueOf(2).equals(type)) {
				url.append(shopGoodsDetail).append(targetId);
			} else if (Integer.valueOf(3).equals(type)) {
				url.append(shopClassifyDetail).append(targetId);
			}
			return AjaxResult.success((Object) url.toString());
		} catch (Exception e) {
			logger.error("获取导购商品主题链接异常", e);
			return AjaxResult.error("获取导购商品主题链接异常");
		}
	}
}
